/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.controller;



import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.LinkedHashMap;
import java.util.Map;


@RestController
public class ResourcesHomeController {


	@RequestMapping(value="/hello")
	public Map<String, String> user(Principal principal) {
		Map<String, String> map = new LinkedHashMap<>();
		map.put("name", principal.getName());
		return map;
	}
	//需要写增删改查，并且最好根据不同的进行操作分类
	//使用MY sql JPA对单边进行操作
	//再做个服务调用这个服务
	//zuul外的服务调用这些服务
	//最后实现JWT

	@RequestMapping(value = "/",method = RequestMethod.GET)
	@ResponseBody
	public String helloWorld(Principal principal) {
		return principal == null ? "Hello anonymous" : "Hello " + principal.getName();
	}

	@PreAuthorize("#oauth2.hasScope('openid') and hasRole('ROLE_ADMIN')")
	@RequestMapping(value = "/secret", method = RequestMethod.GET)
	@ResponseBody
	public String helloSecret(Principal principal) {
		return principal == null ? "Hello anonymous" : "S3CR3T  - Hello " + principal.getName();
	}

}
